using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Voodoo.Pattern;

public class TransitionMachine : MonoBehaviour
{
	public List<GameObject> ProgressParts;

	public TransitionCoin _coinPrefab;

	public Transform _spawnPoint;

	public float multiplier;

	public List<TransitionCoin> SpawnedCoins;

	private float CoinSize = 500f;

	private bool firstCoin = true;

	public float currentValue;

	private void Awake()
	{
		ProgressParts.SetActive(isActive: false);
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStart));
	}

	private void OnDestroy()
	{
	}

	private void OnGameStart()
	{
		if (SpawnedCoins != null)
		{
			while (SpawnedCoins.Count > 0)
			{
				TransitionCoin transitionCoin = SpawnedCoins[0];
				SpawnedCoins.RemoveAt(0);
				UnityEngine.Object.Destroy(transitionCoin.gameObject);
			}
		}
	}

	private void CalculateCoinSize()
	{
		int num = (int)((float)(2 * SingletonMB<Progress>.Instance.GetMaxCount()) * multiplier);
		CoinSize = (num / 15).Round(50);
	}

	public async void CollectedCoin(float value)
	{
		if (SingletonMB<GameManager>.Instance.finised)
		{
			return;
		}
		if (firstCoin)
		{
			CalculateCoinSize();
			firstCoin = false;
		}
		await Task.Delay(1000);
		currentValue += value;
		int num = (int)Mathf.Lerp(0f, ProgressParts.Count, currentValue / CoinSize);
		if (ProgressParts != null && ProgressParts.Count > 0)
		{
			for (int i = 0; i < ProgressParts.Count; i++)
			{
				if (ProgressParts[i] != null)
				{
					ProgressParts[i].SetActive(i < num);
				}
			}
		}
		int num2 = (int)CoinSize;
		if (currentValue > (float)num2)
		{
			currentValue -= num2;
			CreateCoin();
			ProgressParts.SetActive(isActive: false);
		}
	}

	private async void CreateCoin()
	{
		await Task.Delay(250);
		if (!SingletonMB<GameManager>.Instance.finised)
		{
			TransitionCoin transitionCoin = UnityEngine.Object.Instantiate(_coinPrefab);
			transitionCoin._cointext.text = ((int)CoinSize).Round(100).ToString() ?? "";
			transitionCoin.transform.SetParent(_spawnPoint);
			transitionCoin.transform.localScale *= UnityEngine.Random.Range(1f, 1.5f);
			transitionCoin.transform.position = _spawnPoint.transform.position;
			transitionCoin._connectRigid.simulated = true;
			SpawnedCoins.Add(transitionCoin);
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
		}
	}
}
